import 'dart:math';

import 'package:lib_database/lib_database.dart';
import 'package:lib_database/objectbox.dart';

class MnemonicData {
  late int _wallet_id;
  int get walletId => _wallet_id;

  static final _instance = MnemonicData.init();
  MnemonicData.init();

  factory MnemonicData() {
    return _instance;
  }

  setWalletId(int id) {
    _wallet_id = id;
  }

  static String getMnemonic(int wallet_id) {
    List<Wallet> list = ObjectBox().walletModel.getWalletList();
    Wallet wallet = list.where((item) => item.id == wallet_id).toList().first;
    // 解密
    String key = ObjectBox().decryptWalletKey(wallet.key);
    return key;
  }

  static List<String> getMnemonicList(int wallet_id) {
    var key = getMnemonic(wallet_id);
    return key.split(' ');
  }

  static List<String> disrupteKey(String key) {
    List<String> words = key.split(' ');
    final random = Random();
    for (var i = words.length - 1; i > 0; i--) {
      var j = random.nextInt(i + 1);
      var temp = words[i];
      words[i] = words[j];
      words[j] = temp;
    }
    return words;
  }
}
